package com.qianfeng.goods.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qianfeng.goods.bean.Goods;
import com.qianfeng.goods.bean.TypeBean;
import com.qianfeng.goods.service.GoodsService;
import com.qianfeng.goods.service.TypeService;
import com.qianfeng.user.bean.User;
import com.qianfeng.utils.Pagetion;
import com.qianfeng.utils.Response;

@Controller
@RequestMapping("/goods/")
public class GoodsController {
	GoodsService goodsService = new GoodsService();
	TypeService typeService = new TypeService();

	@RequestMapping("totype")
	public String toTypes(HttpSession session, TypeBean bean, HttpServletRequest req) {
		User u = (User) session.getAttribute("user");

		if (u != null) {
			// 分页的用法,
			// 1. 导入jar , 让Javabean继承基础类 Page
			// 2.获取客户端传递来的参数
			// 3.获取符合条件的数据总量
			// 4.计算拼接分页的html代码(currentPage,pageSize,count)
			// 分页方法会计算出一个start值,反哺给javabean, 方法最后返回给我们一个 拼接好的html代码, 我们放到jsp页面中即可

			// 使用自定义的封装工具映射参数

			int count = typeService.getCount(bean); // 获取数据的总量
			// 设置分页
			String paging = Pagetion.paging(bean, count, "/shopback/goods/totype", req.getParameterMap());
			req.setAttribute("paging", paging);

			// 在真正查数据之前, 一定要先去看看总共有多少条数据
			List<TypeBean> list = typeService.getList(bean);

			req.setAttribute("typeList", list);

			req.setAttribute("bean", bean);

			return "types";
		} else {
			return "login";
		}
	}

	@ResponseBody
	@RequestMapping("getTypeById")
	public TypeBean getTypeById(HttpSession session, HttpServletRequest req) {
		User u = (User) session.getAttribute("user");

		if (u != null) {
			// 将接收到的string型的id转成long型之后,再传递给service层
			String id = req.getParameter("id");
			TypeBean bean = typeService.getById(Long.parseLong(id));
			return bean;
		} else {
			return null;
		}
	}

	@RequestMapping("deleteType")
	public String deleteType(HttpServletRequest req, HttpSession session) {
		User u = (User) session.getAttribute("user");

		if (u != null) {
			// 将接收到的string型的id转成long型之后,再传递给service层
			String id = req.getParameter("id");
			typeService.deleteById(Long.parseLong(id));

			return "redirect:/goods/totype";

		} else {
			return "login";
		}
	}

	@ResponseBody
	@RequestMapping("updateType")
	public Response updateType(HttpSession session, HttpServletRequest req, TypeBean bean) {
		User u = (User) session.getAttribute("user");
		Response rs = new Response();
		if (u != null) {

			bean.setUser_id(u.getId_());

			boolean flg = typeService.updateType(bean);
			if (flg) {
				rs.setCode(1);
			} else {
				rs.setCode(-1);
				rs.setMessage("对不起, 操作失败");
			}
			return rs;

		} else {
			return null;
		}
	}

	@RequestMapping("toGoodsPage")
	public String toGoodsPage(HttpSession session, HttpServletRequest req, Goods goods) {
		User u = (User) session.getAttribute("user");

		if (u != null) {

			int count = goodsService.getCount(goods);

			String paging = Pagetion.paging(goods, count, "/shopback/goods/toGoodsPage", req.getParameterMap());

			req.setAttribute("paging", paging);

			List<Goods> list = goodsService.getList(goods);

			req.setAttribute("list", list);

			// 查询出所有的分类
			List<TypeBean> typeList = typeService.getListAll();
			req.setAttribute("typeList", typeList);

			req.setAttribute("goods", goods);

			return "goods";
		} else {
			return "login";
		}
	}

	@RequestMapping("toAddGoodsPage")
	public String toAddGoodsPage(HttpSession session, HttpServletRequest req) {
		User u = (User) session.getAttribute("user");

		if (u != null) {

			// 获取所有的商品分类
			List<TypeBean> list = typeService.getListAll();
			req.setAttribute("types", list);

			// 如果客户端传来了商品id, 我们认为,用户是要修改某个商品
			String s = req.getParameter("id");
			if (s != null) {
				long id = Long.parseLong(s);
				Goods goods = goodsService.getById(id);

				req.setAttribute("goods", goods);
			}

			return "addGoods";
		} else {
			return "login";
		}
	}

	@ResponseBody
	@RequestMapping("updateGoods")
	public Response updateGoods(HttpSession session, Goods goods) {
		User u = (User) session.getAttribute("user");
		Response rs = new Response();
		if (u != null) {
			// 将接收到的string型的id转成long型之后,再传递给service层

			goods.setUser_id(u.getId_());
			boolean flg = goodsService.updateGoods(goods);

			if (flg) {
				rs.setCode(1);
				rs.setMessage("操作成功");
			} else {
				rs.setCode(-1);
				rs.setMessage("操作失败");
			}
			return rs;

		} else {
			return null;
		}

	}

	@RequestMapping("updateGoodsStatus")
	public String updateGoodsStatus(HttpSession session, Goods goods) {
		User u = (User) session.getAttribute("user");
		if (u != null) {
			// 将接收到的string型的id转成long型之后,再传递给service层

			goods.setUser_id(u.getId_());

			goodsService.updateStatus(goods);

			return "redirect:/goods/toGoodsPage";

		} else {
			return "login";
		}
	}

}
